package com.lagou.minicat.core;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Connector {

    private int port;
    private Mapper mapper;
    private int processorCount = 5; // FIXME 线程数配置传入
    private static ExecutorService PROCESSOR_POOL;

    public Connector(int port, Mapper mapper) {
        this.port = port;
        this.mapper = mapper;
        PROCESSOR_POOL = Executors.newFixedThreadPool(5);
    }

    public void start() throws Exception {
        // 启动socket监听请求
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Minicat running on port: " + port);
        while (true) {
            Socket socket = serverSocket.accept();
            PROCESSOR_POOL.submit(new RequestProcessor(socket, mapper));
        }
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Mapper getMapper() {
        return mapper;
    }

    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    public int getProcessorCount() {
        return processorCount;
    }

    public void setProcessorCount(int processorCount) {
        this.processorCount = processorCount;
    }
}
